package com.itic.mall.system.product.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.category.persistence.dao.CategoryDao;
import com.itic.mall.system.category.persistence.model.Category;
import com.itic.mall.system.product.persistence.dao.ProductDao;
import com.itic.mall.system.product.persistence.model.Product;
import com.itic.mall.system.product.persistence.model.ProductExample;
import com.itic.mall.system.productsku.persistence.dao.ProductSkuDao;
import com.itic.mall.system.productsku.persistence.model.ProductSku;
import com.itic.mall.system.productsku.persistence.model.ProductSkuExample;
import com.itic.mall.system.productsku.persistence.model.ProductSkuExample.Criteria;


@Service
@Transactional
public class ProductService
     extends CrudService<ProductDao,Product,ProductExample> {
	@Autowired
	private CategoryDao categoryDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private ProductSkuDao productSkuDao;

	public String addAndProductSku(Category category, Product product, ProductSku productSku) {
		//将全部商品分类 也要加1
		Category allCategory = categoryDao.selectByPrimaryKey("0");
		if( null != allCategory ){
			String totalCount = "0";
			if( StringHelper.isNotBlank(allCategory.getCount()) ){
				totalCount = allCategory.getCount();
			}
			totalCount = NumberStringHelper.addString(totalCount, "1");
			allCategory.setCount(totalCount);
			categoryDao.updateByPrimaryKeySelective(allCategory);
		}
		categoryDao.updateByPrimaryKeySelective(category);
		productDao.insert(product);
		productSkuDao.insert(productSku);
		return SUCCESS;
	}

	public String deleteAndSku(Category category,Product temp) {
		ProductSkuExample example = new ProductSkuExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andProductIdEqualTo(temp.getId());
		ProductSku sku = new ProductSku();
		sku.setIsEffective(DBConst.FALSE);
		
		//将全部商品分类 也要加1
		Category allCategory = categoryDao.selectByPrimaryKey("0");
		if( null != allCategory ){
			String totalCount = "0";
			if( StringHelper.isNotBlank(allCategory.getCount()) ){
				totalCount = allCategory.getCount();
			}
			totalCount = NumberStringHelper.subtractString(totalCount, "1");
			allCategory.setCount(totalCount);
			categoryDao.updateByPrimaryKeySelective(allCategory);
		}
		
		categoryDao.updateByPrimaryKeySelective(category);
		productSkuDao.updateByExampleSelective(sku, example);
		productDao.updateByPrimaryKeySelective(temp);
		return SUCCESS;
	}
	
	/**
	 * 根据商品ID 查找全部款式
	 * @param id
	 * @return
	 */
	public List<ProductSku> getSkusByProductId( String id ){
		List<ProductSku> result = null;
		if( StringHelper.isNotBlank(id) ){
			ProductSkuExample example = new ProductSkuExample();
			example.setOrderByClause("CREATE_TIME DESC");
			example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andProductIdEqualTo(id);
			result = productSkuDao.selectByExample(example);
		}
		return result;
	}


}